import ajax from '@/utils/ajax'

const type2nameMap = {
  // radio: 'SingleChoose',
  // checkbox: 'MultiChoose',
  // blank: 'Blank'
}
const appId = 'wxf2ab3d711f0dfe27'
const companyId = 8
const handleList = (list) => {
  list.forEach(item => {
    item.componentName = type2nameMap[item.questionType] || item.questionType
    try {
      item.questionContent = JSON.parse(item.questionContent)
    } catch (e) {
      console.error('json转换出错了', e)
    }
  })
  return list
}
export const getQuestions = async () => {
  try {
    const res = await ajax.get('/survey/minimall/question/get-list-by-appid.do', {
      params: {
        appId,
        companyId
      }
    })
    // console.log('res', res)

    res.list = handleList(res.list)
    return res.list
  } catch (e) {
    // console.log('e', e)
  }
}

export const submit = async (data) => {
  data.appId = appId
  data.companyId = companyId
  try {
    const res = await ajax.post('/survey/minimall/question/save.do', {
      data
    })
    // console.log('res', res)

    return res
  } catch (e) {
    // console.log('e', e)
  }
}
